//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "ddk.h"
#include <stdlib.h>
#include <misc.h>
#include <hash.h>
#include <fat.h>
#include <ChangeNotifier.h>

fatfs_sharedata::fatfs_sharedata()
{
    clsize = 0;
    dirents = 0;
    root_cluster = 0;
    fat_dirty = 0;
    fat_size = 0;
    nclust = 0;
    data0 = 0;
    rootdir = NULL;
    procroot = NULL;
    dirhash = NULL;
    pBlkDev = NULL;
    pNotifier = NULL;
    m_nRef = 0;
    m_sdCardEjected = 0;
    bpb_base = 0;
    pSyncBufEvent = NULL;
    bExit = FALSE;
    secsz = 512;
    secshift = 9;
}

fatfs_sharedata::~fatfs_sharedata()
{
    if (procroot) {
        if (procroot->n_files)
            hash_dealloc(procroot->n_files);
        if (procroot->n_clust) {
            if ((procroot->n_clust)->c_clust)
                free((procroot->n_clust)->c_clust);
            free(procroot->n_clust);
        }
        free(procroot);
    }

    if (dirhash)
        hash_dealloc(dirhash);
    if (pNotifier)
        delete pNotifier;
    if (pSyncBufEvent)
        pSyncBufEvent->Release();
}

UInt32 fatfs_sharedata::AddRef()
{
    return InterlockedIncrement((PInt32)&m_nRef);
}

UInt32  fatfs_sharedata::Release()
{
    UInt32 Refs;

    Refs = InterlockedDecrement((PInt32)&m_nRef);
    if (Refs == 0) {
        delete this;
    }
    return Refs;
}
